from typing import List

## https://leetcode.cn/problems/lexicographical-numbers/solutions/1429714/by-jam007-kerp/

class Solution:

    def lexicalOrder(self, n: int) -> List[int]:
        ans = [0] * n
        num = 1
        for i in range(n):
            ans[i] = num
            if num * 10 <= n:
                num *= 10
            else:
                while num % 10 == 9 or num + 1 > n:
                    num //= 10
                num += 1
        return ans

##sorted函数详解(高级篇)
##https://www.jb51.net/article/147635.htm
    def lexicalOrder2(self, n: int) -> List[int]:
        return sorted(range(1,n+1), key=str)
